package com.example.combwx.mvp.view.fragment;


import android.content.Context;
import android.content.Entity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.combwx.R;
import com.example.combwx.db.UserUtility;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.view.BaseActivity;
import com.example.combwx.mvp.base.view.BaseFragment;
import com.example.combwx.mvp.contract.RegisterContract;
import com.example.combwx.mvp.di.DaggerRegisterComponent;
import com.example.combwx.mvp.di.RegisterModels;
import com.example.combwx.mvp.model.HttpCode;
import com.example.combwx.mvp.model.entity.ResponseUserEntity;
import com.example.combwx.mvp.model.entity.UserEntity;
import com.example.combwx.mvp.presenter.RegisterPresenter;
import com.example.combwx.mvp.view.ChangeinformationActivity;
import com.example.combwx.utils.LogUtils;
import com.example.combwx.utils.SpUtils;
import com.example.combwx.utils.ToasetUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class AccountFragment extends BaseFragment<RegisterPresenter> implements RegisterContract.IRegisterView {
    private static final String TAG = "AccountFragment";

    private EditText account_username;
    private EditText account_pwd;
    private Button account_login_but;

    public AccountFragment() {
        // Required empty public constructor
    }


//    @Override
//    public int addColor() {
//        return Color.WHITE;
//    }
//    @Override
//    public View addStatusView() {
//        return f(R.id.login_linear);
//    }



    @Override
    public int bindLayout() {
        return R.layout.fragment_account;
    }

    @Override
    public void initView() {
        account_username = f(R.id.account_username);
        account_pwd = f(R.id.account_pwd);
        account_login_but = f(R.id.account_login_but);
        account_login_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "onClick: ");
                String username = account_username.getText().toString().trim();
                String pwd = account_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(username)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(getActivity(), "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    SpUtils.accounData("user",true);
                    p.getLogin(username,pwd,HttpCode.LOGINCODE);
                }
            }
        });

    }

    @Override
    public void initData() {

    }

    @Override
    public void inject(){
        DaggerRegisterComponent.builder()
                .registerModels(new RegisterModels(this))
                .build().inject(this);
    }

    @Override
    public void regirefres(BaseEntity entity) {
        LogUtils.show("regirefres"+entity);
        if (entity.status.equals("200")){
            SpUtils.accounFatch();
        }
        ResponseUserEntity responseUserEntity= (ResponseUserEntity) entity;
        com.blankj.utilcode.util.LogUtils.json(responseUserEntity);
        Intent intent = new Intent();
        intent.setAction("nickname");
        intent.putExtra("userValues",responseUserEntity.getValues());
        getActivity().sendBroadcast(intent);
        getActivity().finish();
    }
}
